package eu.telecombretagne.rsm.gui.frames;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import eu.telecombretagne.rsm.gui.NameConsts;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogInternalFrame extends JInternalFrame implements ActionListener {
	private static final long serialVersionUID = -7330323228081308675L;
	
	static final int xOffset = 10, yOffset = 5;
	
	private SimpleAttributeSet emitterKeyword;
	private SimpleAttributeSet errorKeyword;
	private SimpleAttributeSet infoKeyword;
	private SimpleAttributeSet networkKeyword;
	private SimpleAttributeSet receiverKeyword;
	
	private JTextPane networkLog;
	
	private SimpleDateFormat logTimeFormatter;
	
	/**
	 * The constructor of the LogInternalFrame.
	 */
	public LogInternalFrame(){
		super(NameConsts.SYSTEMLOG,
				true,	//resizable
				true,	//closable
				true,	//maximizable
				true);	//iconifiable
		
		setSize(600,600);
		setFrameIcon(new ImageIcon(this.getClass().getResource(NameConsts.GFXLOG)));
		setLocation(xOffset, yOffset);
		
		logTimeFormatter = new SimpleDateFormat(NameConsts.logTimeFormat);
		
		networkLog = initializeLogTextPane();
		add(new JScrollPane(networkLog), BorderLayout.CENTER);
		
		initializeKeywords();
	}
	
	private JTextPane initializeLogTextPane(){
		JTextPane textPane = new JTextPane();
		textPane.setMargin(new Insets(5,5,5,5));
		textPane.setEditable(false);
		
		return textPane;
	}
	
	private void initializeKeywords(){
		emitterKeyword = initializeKeyword(Color.RED, Color.WHITE, false);
		errorKeyword = initializeKeyword(Color.WHITE, Color.RED, true);
		infoKeyword = initializeKeyword(Color.GRAY, Color.WHITE, false);
		networkKeyword = initializeKeyword(Color.BLUE, Color.WHITE, false);
		receiverKeyword = initializeKeyword(Color.GREEN, Color.WHITE, false);
	}
	
	private SimpleAttributeSet initializeKeyword(Color pForeground, Color pBackground, Boolean pBold){
		SimpleAttributeSet keyword = new SimpleAttributeSet();
		
		StyleConstants.setForeground(keyword, pForeground);
		StyleConstants.setBackground(keyword, pBackground);
		StyleConstants.setBold(keyword, pBold);
		
		return keyword;
	}
	
	private synchronized void appendLineToLog(String pLine, String pTag, SimpleAttributeSet pKeyword) {
		String line = logTimeFormatter.format(new Date()) + " [" + pTag + "] "+ pLine + "\n";
		StyledDocument doc = networkLog.getStyledDocument();
		
		try {
			doc.insertString(doc.getLength(), line, pKeyword);
			System.out.println(line);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		networkLog.setCaretPosition(doc.getLength());
	}
	
	/**
	 * Appends a line to the emitter log
	 * @param pLine A string line to be appended.
	 */
	public void appendLineEmitterToLog(String pLine){
		appendLineToLog(pLine, NameConsts.EMITTER, emitterKeyword);
	}
	
	/**
	 * Appends a line to the error log
	 * @param pLine A string line to be appended.
	 */
	public void appendLineErrorToLog(String pLine){
		appendLineToLog(pLine, NameConsts.ERROR, errorKeyword);
	}
	
	/**
	 * Appends a line to the info log
	 * @param pLine A string line to be appended.
	 */
	public void appendLineInfoToLog(String pLine){
		appendLineToLog(pLine, NameConsts.INFO, infoKeyword);
	}
	
	/**
	 * Appends a line to the network log
	 * @param pLine A string line to be appended.
	 */
	public void appendLineNetworkToLog(String pLine){
		appendLineToLog(pLine, NameConsts.NETWORK, networkKeyword);
	}
	
	/**
	 * Appends a line to the receiver log
	 * @param pLine A string line to be appended.
	 */
	public void appendLineReceiverToLog(String pLine){
		appendLineToLog(pLine, NameConsts.RECEIVER, receiverKeyword);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}